dnl
dnl %CopyrightBegin%
dnl
dnl SPDX-License-Identifier: Apache-2.0
dnl
dnl Copyright Ericsson AB 2005-2025. All Rights Reserved.
dnl
dnl Licensed under the Apache License, Version 2.0 (the "License");
dnl you may not use this file except in compliance with the License.
dnl You may obtain a copy of the License at
dnl
dnl     http://www.apache.org/licenses/LICENSE-2.0
dnl
dnl Unless required by applicable law or agreed to in writing, software
dnl distributed under the License is distributed on an "AS IS" BASIS,
dnl WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dnl See the License for the specific language governing permissions and
dnl limitations under the License.
dnl
dnl %CopyrightEnd%
dnl

dnl define([AC_CACHE_LOAD], )dnl
dnl define([AC_CACHE_SAVE], )dnl


dnl Process this file with autoconf to produce a configure script.
AC_INIT
AC_CONFIG_SRCDIR([c_src/odbcserver.c])
AC_PREREQ([2.71])

m4_include([otp.m4])

AC_CONFIG_AUX_DIR([${ERL_TOP}/make/autoconf])

ERL_PUSH_WERROR

ERL_CANONICAL_SYSTEM_TYPE

AC_ARG_WITH(odbc,
[  --with-odbc=PATH        specify location of ODBC include and lib
  --with-odbc             use ODBC (default)
  --without-odbc          don't use ODBC])

ERL_XCOMP_SYSROOT_INIT

dnl Checks for programs.
AC_PROG_CC
LM_PROG_LD

dnl ---------------------------------------------------------------------
dnl Special windows stuff regarding CFLAGS and details in the environment...
dnl ---------------------------------------------------------------------
LM_WINDOWS_ENVIRONMENT

AC_PROG_MAKE_SET

ERL_OSSF_FLAGS

AC_SUBST(LD)

_search_path=/bin:/usr/bin:/usr/local/bin:$PATH

AC_PATH_PROG(RM, rm, false, $_search_path)
if test "$ac_cv_path_RM" = false; then
  AC_MSG_ERROR([No 'rm' command found])
fi

_search_path=

AS_IF([test "$with_odbc" = "no"],
 [

   rm -f "$ERL_TOP/lib/odbc/SKIP"

 ],
 [
  dnl "$with_odbc" != "no"


# Sockets
#--------------------------------------------------------------------
#	Check for the existence of the -lsocket and -lnsl libraries.
#	The order here is important, so that they end up in the right
#	order in the command line generated by make.  Here are some
#	special considerations:
#	1. Use "connect" and "accept" to check for -lsocket, and
#	   "gethostbyname" to check for -lnsl.
#	2. Use each function name only once:  can't redo a check because
#	   autoconf caches the results of the last check and won't redo it.
#	3. Use -lnsl and -lsocket only if they supply procedures that
#	   aren't already present in the normal libraries.  This is because
#	   IRIX 5.2 has libraries, but they aren't needed and they're
#	   bogus:  they goof up name resolution if used.
#	4. On some SVR4 systems, can't use -lsocket without -lnsl too.
#	   To get around this problem, check for both libraries together
#	   if -lsocket doesn't work by itself.
#--------------------------------------------------------------------
erl_checkBoth=0
AC_CHECK_FUNC(connect, erl_checkSocket=0, erl_checkSocket=1)
AS_IF([test "$erl_checkSocket" = 1],
  [
    AC_CHECK_LIB(socket, socket, LIBS="$LIBS -lsocket", erl_checkBoth=1)
  ])
AS_IF([test "$erl_checkBoth" = 1],
  [
    tk_oldLibs=$LIBS
    LIBS="$LIBS -lsocket -lnsl"
    AC_CHECK_FUNC(accept, odbc_erl_checkNsl=0, [LIBS=$tk_oldLibs])
  ])
AC_CHECK_FUNC(gethostbyname, , AC_CHECK_LIB(nsl, main, [LIBS="$LIBS -lnsl"]))
AS_CASE(["$host_os"],
        [haiku*],
        [
		AC_CHECK_LIB(network, socket)
        ])

dnl Checks for header files.
AC_CHECK_INCLUDES_DEFAULT
AC_PROG_EGREP

AC_CHECK_HEADERS([fcntl.h netdb.h stdlib.h string.h sys/socket.h winsock2.h])
AC_CHECK_HEADERS([windows.h])

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_SIZE_T
AC_CHECK_MEMBERS([struct sockaddr_in6.sin6_addr], [], [],
			 [#if HAVE_WINSOCK2_H
			  #include <winsock2.h>
			  #include <ws2tcpip.h>
			  #else
			  #include <netinet/in.h>
			  #endif])

dnl Checks for library functions.
AC_CHECK_FUNCS([memset socket])

# ODBC
$RM -f "$ERL_TOP/lib/odbc/SKIP"

LM_CHECK_THR_LIB
AC_SUBST(THR_DEFS)
AC_SUBST(THR_LIBS)

odbc_lib_link_success=no
AC_SUBST(TARGET_FLAGS)
AS_CASE([$host_os],

        [darwin1[[0-9]].*],
        [
                TARGET_FLAGS="-DUNIX"
	        if test ! -d "$with_odbc" || test "$with_odbc" = "yes"; then
		    ODBC_LIB= -L"/usr/local/lib"
		    ODBC_INCLUDE="-I/usr/local/include"
		else
		    ODBC_LIB=-L"$with_odbc/lib"
		    ODBC_INCLUDE="-I$with_odbc/include"
		fi

               save_LIBS="$LIBS"
               LIBS="$LIBS $ODBC_LIB"
               AC_CHECK_LIB(iodbc, SQLAllocHandle,[ODBC_LIB="$ODBC_LIB -liodbc"; odbc_lib_link_success=yes])
               if test $odbc_lib_link_success = no; then
                  AC_CHECK_LIB(odbc, SQLAllocHandle,[ODBC_LIB="$ODBC_LIB -lodbc"; odbc_lib_link_success=yes])
               fi
               LIBS="$save_LIBS"
        ],

        [haiku*],
        [
               TARGET_FLAGS="-DUNIX"
               ODBC_LIB= -L"/system/lib"
               ODBC_INCLUDE="-I/system/develop/headers"
               dnl Haiku's package manager will deal with this for us
               save_LIBS="$LIBS"
               LIBS="$LIBS $ODBC_LIB"
               AC_CHECK_LIB(odbc, SQLAllocHandle,[ODBC_LIB="$ODBC_LIB -lodbc"; odbc_lib_link_success=yes])
               if test $odbc_lib_link_success = no; then
                  AC_CHECK_LIB(iodbc, SQLAllocHandle,[ODBC_LIB="$ODBC_LIB -liodbc"; odbc_lib_link_success=yes])
               fi
               LIBS="$save_LIBS"
        ],

        [win32|cygwin],
        [
		TARGET_FLAGS="-DWIN32"
		AC_CHECK_LIB(ws2_32, main)
		if test ! -d "$with_odbc" || test "$with_odbc" = "yes"; then
		    ODBC_LIB=""
		    ODBC_INCLUDE=""
		else
		    ODBC_LIB=-L"$with_odbc/lib"
		    ODBC_INCLUDE="-I$with_odbc/include"
		fi
		save_LIBS="$LIBS"
		LIBS="$LIBS $ODBC_LIB"
		AC_CHECK_LIB(odbc32, main, [ODBC_LIB="$ODBC_LIB -lodbc32"; odbc_lib_link_success=yes])
		LIBS="$save_LIBS"
        ],

        [
	        TARGET_FLAGS="-DUNIX"
		AS_CASE(["$erl_xcomp_without_sysroot-$with_odbc"],

                    [yes-yes | yes-],
                    [
			msg="Dont know where to search for odbc (setting erl_xcomp_sysroot will help)"
			AC_MSG_WARN([$msg])
			echo "$msg" > "$ERL_TOP/lib/odbc/SKIP"
			odbc_lib_link_success=wont_try
                    ],

                    [no-yes | no-],
                    [
			AC_CHECK_SIZEOF(void *)
			AC_MSG_CHECKING([for odbc in standard locations])
			for rdir in /usr/local/odbc /usr/local /usr/odbc \
			            /usr /opt/local/pgm/odbc /usr/local/pgm/odbc \
			            "$with_odbc"; do
			  test -f "$erl_xcomp_isysroot$rdir/include/sql.h" || continue
			  is_odbc_std_location=yes
			  libdir="$erl_xcomp_sysroot$rdir/lib"
			  if test "$ac_cv_sizeof_void_p" = "8"; then
			      dnl "/." in test is important (dir symlinks)
			      if test -d "${libdir}64/."; then
				  libdir="${libdir}64"
			      elif test -d "${libdir}/64/."; then
				  libdir="${libdir}/64"
			      fi
			  fi
			  ODBC_LIB="-L$libdir"
			  ODBC_INCLUDE="-I$erl_xcomp_isysroot$rdir/include"
			  break
			done
			if  test "x$is_odbc_std_location" != "xyes"; then
			    AC_MSG_RESULT(no)
			    AC_MSG_WARN([No odbc library found skipping odbc])
			    echo "No odbc library found" > "$ERL_TOP/lib/odbc/SKIP"
			else
			    AC_MSG_RESULT($ODBC_LIB)
			    save_LIBS="$LIBS"
			    LIBS="$LIBS $ODBC_LIB"
			    AC_CHECK_LIB(odbc, SQLAllocHandle,[ODBC_LIB="$ODBC_LIB -lodbc"; odbc_lib_link_success=yes])
			    if test $odbc_lib_link_success = no; then
			       AC_CHECK_LIB(iodbc, SQLAllocHandle,[ODBC_LIB="$ODBC_LIB -liodbc"; odbc_lib_link_success=yes])
			    fi
			    LIBS="$save_LIBS"
			fi
                    ],

                    [
			ODBC_LIB=-L"$with_odbc/lib"
			ODBC_INCLUDE="-I$with_odbc/include"
			save_LIBS="$LIBS"
			LIBS="$LIBS $ODBC_LIB"
			AC_CHECK_LIB(odbc, SQLAllocHandle,[ODBC_LIB="$ODBC_LIB -lodbc"; odbc_lib_link_success=yes])
			if test $odbc_lib_link_success = no; then
			   AC_CHECK_LIB(iodbc, SQLAllocHandle,[ODBC_LIB="$ODBC_LIB -liodbc"; odbc_lib_link_success=yes])
			fi
			LIBS="$save_LIBS"
                    ])
        ])

save_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS $ODBC_INCLUDE"
AC_CHECK_HEADERS([sql.h sqlext.h], [odbc_required_headers=yes], [odbc_required_headers=no],
[[#ifdef HAVE_WINDOWS_H
     # include <windows.h>
     #endif
     ]])
CFLAGS="$save_CFLAGS"

if test $odbc_required_headers = no;  then
    AC_MSG_WARN(["ODBC library - header check failed"])
    echo "ODBC library - header check failed" > $ERL_TOP/lib/odbc/SKIP
fi
if test $odbc_lib_link_success = no;  then
    AC_MSG_WARN(["ODBC library - link check failed"])
    echo "ODBC library - link check failed" > $ERL_TOP/lib/odbc/SKIP
fi

AC_SUBST(ODBC_LIB)
AC_SUBST(ODBC_INCLUDE)

 ]) dnl "$with_odbc" != "no"

dnl ----------------------------------------------------------------------
dnl Check for GCC diagnostic ignored "-Wstringop-truncation"
dnl ----------------------------------------------------------------------
saved_CFLAGS="$CFLAGS"
CFLAGS="-Werror $CFLAGS"
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[_Pragma("GCC diagnostic push")
         _Pragma("GCC diagnostic ignored \"-Wstringop-truncation\"")
         _Pragma("GCC diagnostic pop")
         ]])],[AC_DEFINE(HAVE_GCC_DIAG_IGNORE_WSTRINGOP_TRUNCATION,1,
           define if compiler support _Pragma('GCC diagnostic ignored '-Wstringop-truncation''))],[])
CFLAGS="$saved_CFLAGS"

AS_IF([test "x$GCC" = xyes],
  [
    # Treat certain GCC warnings as errors
    LM_TRY_ENABLE_CFLAG([-Werror=return-type], [CFLAGS])
  ])

ERL_POP_WERROR

AC_CONFIG_FILES([c_src/$host/Makefile:c_src/Makefile.in])
AC_OUTPUT
